﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;   //   for Texture2D
using Microsoft.Xna.Framework;  //  for Vector2


using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace RisingThreat
{
    class Bullet
    {
        public Model model;
        public float scale = 0.05f;
        public BoundingSphere bounding;
        public bool isAlive = false;
        public float speed = 4.0f;
        public Vector3 position;
        public Vector3 velocity;
        public float orientation;

        public Bullet(Model m)
        {
            model = m;
            bounding.Radius = 0.05f;
        }

        public void Update(GameTime elapsedTime, Level level)
        {
            float timeDifference = (float)elapsedTime.ElapsedGameTime.TotalMilliseconds / 1000.0f;
            position += velocity * timeDifference;
            bounding.Center = position;
            bounding.Center.Y = 1.0f;
            CheckForCollisions(level);
        }

        public void CheckForCollisions(Level level)
        {
            CollisionDetector cd = new CollisionDetector();
            HashSet<QuadTreeItem> hashie = level.getPotentialCollisions(bounding);

            List<QuadTreeItem> collisions = cd.detectLevelCollisions(bounding, hashie);
            foreach (QuadTreeItem item in collisions)
            {

                if (bounding.Intersects(item.getBoundingVolume()))
                {
                    ResolveCollisionBVH(item);
                }
            }
        }

        public void ResolveCollisionBVH(QuadTreeItem item)
        {
            isAlive = false;
        }

        public void Draw(GraphicsDeviceManager graphics, Matrix view, Matrix projection)
        {
            if (isAlive)
            {
                foreach (ModelMesh mesh in model.Meshes)
                {
                    foreach (BasicEffect effect in mesh.Effects)
                    {
                        effect.EnableDefaultLighting();
                        effect.PreferPerPixelLighting = true;

                        /*effect.World = 
                            Matrix.CreateFromYawPitchRoll(
                            MathHelper.PiOver2 + orientation,
                            0.0f,
                            MathHelper.Pi) *Matrix.CreateScale(scale)*
                            Matrix.CreateTranslation(position);
                        */
                        effect.World = Matrix.CreateScale(scale) * Matrix.CreateFromYawPitchRoll(
                            orientation + MathHelper.PiOver2,
                            0.0f,
                            0.0f) * Matrix.CreateTranslation(new Vector3(position.X, position.Y, position.Z));
                        effect.Projection = projection;
                        effect.View = view;
                    }
                    mesh.Draw();
                }
            }
        }

        public void Draw2(GraphicsDeviceManager graphics, Matrix view, Matrix projection)
        {
            if (isAlive)
            {
                foreach (ModelMesh mesh in model.Meshes)
                {
                    foreach (BasicEffect effect in mesh.Effects)
                    {
                        effect.EnableDefaultLighting();
                        effect.PreferPerPixelLighting = true;

                        /*effect.World = 
                            Matrix.CreateFromYawPitchRoll(
                            MathHelper.PiOver2 + orientation,
                            0.0f,
                            MathHelper.Pi) *Matrix.CreateScale(scale)*
                            Matrix.CreateTranslation(position);
                        */
                        effect.World = Matrix.CreateScale(scale) * Matrix.CreateFromYawPitchRoll(
                            MathHelper.Pi+orientation/180*MathHelper.Pi,
                            0.0f,
                            0.0f) * Matrix.CreateTranslation(new Vector3(position.X, position.Y, position.Z));
                        effect.Projection = projection;
                        effect.View = view;
                    }
                    mesh.Draw();
                }
            }
        }

    }
}
